package com.fpt.quanlyhethongsan.controller;

import java.text.DecimalFormat;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.fpt.quanlyhethongsan.utils.Constants;
import com.fpt.quanlysanbong.dao.ServiceDao;
import com.fpt.quanlysanbong.domain.SoccerField;
import com.fpt.quanlysanbong.domain.SoccerService;

public class ServiceController {

	private int id;

	private String name;

	private int unitPrice;

	private String operation;

	private SoccerService soccerService;

	private List<SoccerService> listSoccerService;

	private boolean isSuccess;

	private String jsonString;

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(int unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public SoccerService getSoccerService() {
		return soccerService;
	}

	public void setSoccerService(SoccerService soccerService) {
		this.soccerService = soccerService;
	}

	public List<SoccerService> getListSoccerService() {
		return listSoccerService;
	}

	public void setListSoccerService(List<SoccerService> listSoccerService) {
		this.listSoccerService = listSoccerService;
	}

	public String registerService() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		setOperation(Constants.ADD);
		return Constants.SUCCESS;
	}

	public String getListService() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		SoccerField soccerField = (SoccerField) session.getAttribute(Constants.CURRENT_FIELD);
		listSoccerService = new ServiceDao().getListService(soccerField.getId());
		setOperation(Constants.UPDATE);
		return Constants.SUCCESS;
	}

	public String saveService() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		SoccerField soccerField = (SoccerField) session.getAttribute(Constants.CURRENT_FIELD);

		soccerService = new SoccerService();
		soccerService.setName(name);
		soccerService.setUnitPrice(unitPrice);
		soccerService.setSoccerField(soccerField);

		if (StringUtils.equalsIgnoreCase(operation, Constants.ADD)) {
			isSuccess = new ServiceDao().addService(soccerService);
		} else if (StringUtils.equalsIgnoreCase(operation, Constants.UPDATE)) {
			soccerService.setId(id);
			isSuccess = new ServiceDao().editService(soccerService);
		}

		if (isSuccess)
			return Constants.SUCCESS;
		else
			return Constants.ERROR;
	}

	public String deleteService() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		isSuccess = new ServiceDao().deleteService(id);
		if (isSuccess)
			return Constants.SUCCESS;
		else {
			return Constants.ERROR;
		}
	}

	public String getPriceFormat(float number) {
		DecimalFormat f = new DecimalFormat("#,###,###");
		return f.format(number);
	}

	public String getPrice(float number) {
		DecimalFormat f = new DecimalFormat("###");
		return f.format(number);
	}

	public String getUnitPriceOfItem() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		SoccerService sc = new ServiceDao().getSoccerService(id);
		jsonString = getPrice(sc.getUnitPrice());
		return Constants.SUCCESS;
	}
}
